package com.ruoyi.web.controller.common;

//import com.github.promeg.pinyinhelper.Pinyin;
import com.hig.utils.UUIDGenerator;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@RestController
public class TestContorller {


    @Autowired
    private ISysPostService iSysPostService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Autowired
    private ISysUserService iSysUserService;


    @GetMapping("/test/imput")
    public AjaxResult uploadFile() throws Exception {
        try {
            //测试                                            // C:\Users\Administrator\Desktop
            byte[] file = FileUtils.readFileToByteArray(new File("D:\\desktop\\新建文件夹\\人员导入数据\\bbb.xlsx"));
            //   FileUtils.writeByteArrayToFile(new File("C:\\Users\\LRC\\Desktop\\test.txt"),_decode);
            XSSFWorkbook sheets = new XSSFWorkbook(OPCPackage.open(new ByteArrayInputStream(file)));
            XSSFSheet sheet = sheets.getSheetAt(0);
            Stream<Row> stream = StreamSupport.stream(sheet.spliterator(), false);
            String sysUser = SecurityUtils.encryptPassword("123456");
            String code = UUIDGenerator.generate();
            stream.skip(1)
                    .forEach(row -> {
                        SysUser newsysUser = new SysUser();
                        newsysUser.setPassword(sysUser);
                        newsysUser.setUserCode(code);

                        int rowNum = row.getLastCellNum();
                        System.out.println("rowNum------" + rowNum);
                        Cell cell = row.getCell(0);
                        cell = row.getCell(0);
                        String one1 = cell.getStringCellValue();

                        SysDept dept = new SysDept();

                        dept.setDeptName(one1);
                        List<SysDept> sysDepts = iSysDeptService.selectDeptList(dept);

                        System.out.println("部门ID-----" +  sysDepts.get(0).getDeptId());
                        newsysUser.setDeptId(sysDepts.get(0).getDeptId());

                        cell = row.getCell(1);
                        String one2 = cell.getStringCellValue();

                        System.out.println("姓名-----" + one2);
                        newsysUser.setNickName(one2);


                        cell = row.getCell(2);
                        String one3 = cell.getStringCellValue();


                        //处理岗位
                        SysPost post = new SysPost();
                        post.setPostName(one3);
                        List<SysPost> sysPosts = iSysPostService.selectPostList(post);
                        if(sysPosts.size() > 0){



                        }else{
                            SysPost sysPost = new SysPost();
                            sysPost.setPostName(one3);
//                            String piyin = Pinyin.toPinyin(one3, "");
//                            sysPost.setPostCode(piyin);
                            sysPost.setPostSort("1");
                            sysPost.setStatus("0");
                            sysPost.setWorkPermit(0);
                            iSysPostService.insertPost(sysPost);

                        }

                        cell = row.getCell(3);
                        String one4 = "";
                        try {
                            one4 = cell.getStringCellValue();
                        } catch (Exception e) {
                            one4 = String.valueOf(new BigDecimal(cell.getNumericCellValue()).setScale(0, RoundingMode.HALF_UP));
                        }
                        newsysUser.setUserName(one4);
                        System.out.println("账号-----" + one4);
                        newsysUser.setStatus("0");


                        iSysUserService.insertUser(newsysUser);

                    });


            return null;
        } catch (Exception e) {
            new RuntimeException(e);
        }
        return AjaxResult.error();
    }

}
